﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Api.Model;
using Microsoft.IdentityModel.Tokens;

namespace Api.Dal
{
    public class AdminDAL
    {
        public static int GetCount()
        {
            int count = 0;
            EfDemoContext ef = new EfDemoContext();
            if (ef.Comments.IsNullOrEmpty())
            {
                return 0;
            }
            count = ef.Comments.Select(c => c.id).Max();
            return count;
        }
        //public static Admin? GetAdmin(Admin admin)
        //{
        //    EfDemoContext efDemoContext = new EfDemoContext();
        //    Admin? ad = efDemoContext.Admins.Where(a =>
        //        (admin.id == -1 || admin.id == a.id) &&
        //        (admin.name == "N/A" || admin.name == a.name) &&
        //        (admin.password == "N/A" || admin.password == a.password)
        //    ).FirstOrDefault(); 
        //    return ad;
        //}

        public static Admin? GetAdmin(Admin admin)
        {
            EfDemoContext ef = new EfDemoContext();
            Admin? u = ef.Admins.Where(u =>
                (admin.name == "N/A" || string.Equals(u.name, admin.name) &&
                (admin.password == "N/A" || string.Equals(u.password, admin.password))) &&
                (admin.id == -1 || admin.id == u.id)).FirstOrDefault();
            return u;
        }

        public static string insertAdmin(Admin admin)
        {
            string valid = IsValid(admin);
            if (!string.Equals(valid, ""))
            {
                return valid;
            }
            EfDemoContext ef = new EfDemoContext();
            ef.Admins.Add(admin);
            ef.SaveChanges();
            return "添加管理员成功";

        }
        public static bool DeleteAdmin(int id)
        {
            Admin? ad = GetAdmin(new Admin { id = id });
            if (ad != null)
            {
                EfDemoContext ef = new EfDemoContext();
                ef.Admins.Remove(ad);
                ef.SaveChanges();
                return true;
            }
            return false;

        }
        public static bool UpdateAdmin(Admin admin)
        {
            EfDemoContext ef = new EfDemoContext();
            Admin? ad = GetAdmin(new Admin { id = admin.id });
            if (ad == null)
            {
                return false;
            }
            ad.name = admin.name ?? ad.name;
            ad.password = admin.password ?? ad.password;
            ef.Admins.Update(ad);
            ef.SaveChanges();
            return true;
        }
        private static string IsValid(Admin admin)
        {
            string pattern = @"^[A-Za-z0-9]+$";
            Regex regex = new Regex(pattern);
            //用户名判断
            if (string.IsNullOrEmpty(admin.name))
            {
                return "用户名为空";
            }
            bool isValid = regex.IsMatch(admin.name);
            if (!isValid)
            {
                return "用户名不合法";
            }
            //密码判断
            if (string.IsNullOrEmpty(admin.password))
            {
                return "密码为空";
            }
            isValid = regex.IsMatch(admin.password);
            if (!isValid)
            {
                return "密码包含非英文或数字的字符";
            }
            return "";
        }

        public static User forbid(int id)
        {
            User u = UserDal.GetUserById(id);

            if (u.isForbidden == false)
            {
                u.isForbidden = true;
                UserDal.UpdateUser(u);
            }

            return u;
        }

        public static User disForbid(int id)
        {
            User u = UserDal.GetUserById(id);

            if (u.isForbidden == true)
            {
                u.isForbidden = false;
                UserDal.UpdateUser(u);
            }

            return u;
        }
    }
}
